package com.lizard.schedule.ui.activity;

import android.app.Fragment;
import android.os.Bundle;
import android.support.wearable.activity.WearableActivity;
import android.support.wearable.view.CardFragment;
import android.support.wearable.view.GridViewPager;
import android.text.TextUtils;

import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataItem;
import com.google.android.gms.wearable.DataMap;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.Wearable;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lizard.schedule.R;
import com.lizard.schedule.consts.Path;
import com.lizard.schedule.manager.ObserverIfaces;
import com.lizard.schedule.manager.ObserverManager;
import com.lizard.schedule.model.WearSchedule;
import com.lizard.schedule.ui.activity.adapter.ScheduleDetailGridAdapter;
import com.lizard.schedule.ui.activity.base.BaseActivity;
import com.lizard.schedule.ui.activity.base.BaseGoogleApiClientActivity;
import com.lizard.schedule.ui.fragment.OptionsFragment;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangyiqian on 16/6/10.
 */
public class ScheduleDetailActivity extends BaseGoogleApiClientActivity {

    public static final String PARAM_DETAIL = "param_detail";

    private GridViewPager gridViewPager;

    private WearSchedule wearSchedule;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.schedule_detail);

        wearSchedule = getIntent().getParcelableExtra(PARAM_DETAIL);

        gridViewPager = (GridViewPager) findViewById(R.id.pager);

        List<Fragment> fragments = new ArrayList<>();
        String timeTxt = (TextUtils.isEmpty(wearSchedule.getDayNearby()) ? wearSchedule.getDateStr() : wearSchedule.getDayNearby())
                + " " + wearSchedule.getTimeStr();
        CardFragment detailFragment = CardFragment.create(timeTxt, wearSchedule.getContentText());
        fragments.add(detailFragment);

        OptionsFragment optionsFragment = new OptionsFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("wearSchedule", wearSchedule);
        optionsFragment.setArguments(bundle);
        fragments.add(optionsFragment);

        ScheduleDetailGridAdapter adapter = new ScheduleDetailGridAdapter(getFragmentManager(), fragments);
        gridViewPager.setAdapter(adapter);

        ObserverManager.getInstance().addOnSyncSchedulesListener(onSyncSchedulesListener);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ObserverManager.getInstance().removeOnSyncSchedulesListener(onSyncSchedulesListener);
    }

    private ObserverIfaces.OnSyncSchedulesListener onSyncSchedulesListener = new ObserverIfaces.OnSyncSchedulesListener() {
        @Override
        public void onSync(List<WearSchedule> wearSchedules) {
            if(wearSchedules != null){
                boolean hasLocalId = false;
                for(WearSchedule wearSchedule : wearSchedules){
                    if(wearSchedule.getLocalId() == ScheduleDetailActivity.this.wearSchedule.getLocalId()){
                        hasLocalId = true;
                        break;
                    }
                }

                if(!hasLocalId){ // 被删除
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    });
                }
            }
        }
    };
}
